using FluentValidation;
using WeatherSystem.API.Controllers;
using WeatherSystem.API.Models;

namespace WeatherSystem.API.Validators
{
    /// <summary>
    /// 天气数据查询请求验证器
    /// </summary>
    public class WeatherDataQueryRequestValidator : AbstractValidator<Controllers.WeatherDataQueryRequest>
    {
        public WeatherDataQueryRequestValidator()
        {
            RuleFor(x => x.Page)
                .GreaterThan(0).WithMessage("页码必须大于0");

            RuleFor(x => x.PageSize)
                .InclusiveBetween(1, 100).WithMessage("页面大小必须在1-100之间");

            RuleFor(x => x.StartDate)
                .LessThanOrEqualTo(x => x.EndDate)
                .When(x => x.StartDate.HasValue && x.EndDate.HasValue)
                .WithMessage("开始日期不能大于结束日期");

            RuleFor(x => x.SortBy)
                .Must(x => string.IsNullOrEmpty(x) || new[] { "recordedAt", "city" }.Contains(x.ToLower()))
                .WithMessage("排序字段只能是 recordedAt 或 city");

            RuleFor(x => x.SortDirection)
                .Must(x => string.IsNullOrEmpty(x) || new[] { "asc", "desc" }.Contains(x.ToLower()))
                .WithMessage("排序方向只能是 asc 或 desc");
        }
    }

    /// <summary>
    /// 创建动态天气数据请求验证器
    /// </summary>
    public class CreateDynamicWeatherDataRequestValidator : AbstractValidator<CreateDynamicWeatherDataRequest>
    {
        public CreateDynamicWeatherDataRequestValidator()
        {
            RuleFor(x => x.CityId)
                .NotEmpty().WithMessage("城市ID不能为空");

            RuleFor(x => x.RecordedAt)
                .Must(x => x <= DateTime.UtcNow)
                .WithMessage("记录时间不能是未来时间")
                .Must(x => x >= DateTime.UtcNow.AddYears(-1))
                .WithMessage("记录时间不能超过一年前");

            RuleFor(x => x.Source)
                .MaximumLength(50).WithMessage("数据源长度不能超过50个字符");

            RuleForEach(x => x.Metrics.Keys)
                .NotEmpty().WithMessage("指标键不能为空")
                .MaximumLength(100).WithMessage("指标键长度不能超过100个字符")
                .When(x => x.Metrics != null);
        }
    }

    /// <summary>
    /// 更新动态天气数据请求验证器
    /// </summary>
    public class UpdateDynamicWeatherDataRequestValidator : AbstractValidator<UpdateDynamicWeatherDataRequest>
    {
        public UpdateDynamicWeatherDataRequestValidator()
        {
            RuleFor(x => x.RecordedAt)
                .Must(x => !x.HasValue || x.Value <= DateTime.UtcNow)
                .WithMessage("记录时间不能是未来时间")
                .Must(x => !x.HasValue || x.Value >= DateTime.UtcNow.AddYears(-1))
                .WithMessage("记录时间不能超过一年前");

            RuleFor(x => x.Source)
                .MaximumLength(50).WithMessage("数据源长度不能超过50个字符");

            RuleForEach(x => x.Metrics.Keys)
                .NotEmpty().WithMessage("指标键不能为空")
                .MaximumLength(100).WithMessage("指标键长度不能超过100个字符")
                .When(x => x.Metrics != null);
        }
    }

    /// <summary>
    /// 城市对比请求验证器
    /// </summary>
    public class CityComparisonRequestValidator : AbstractValidator<Controllers.CityComparisonRequest>
    {
        public CityComparisonRequestValidator()
        {
            RuleFor(x => x.CityIds)
                .NotEmpty().WithMessage("城市ID列表不能为空")
                .Must(x => x.Count <= 10).WithMessage("最多只能对比10个城市")
                .Must(x => x.Count >= 2).WithMessage("至少需要2个城市进行对比");

            RuleForEach(x => x.CityIds)
                .NotEmpty().WithMessage("城市ID不能为空");
        }
    }

    /// <summary>
    /// 模拟天气请求验证器
    /// </summary>
    public class SimulateWeatherRequestValidator : AbstractValidator<Controllers.SimulateWeatherRequest>
    {
        public SimulateWeatherRequestValidator()
        {
            RuleFor(x => x.BaseTemperature)
                .InclusiveBetween(-50, 60).WithMessage("基础温度必须在-50°C到60°C之间");

            RuleFor(x => x.Count)
                .InclusiveBetween(1, 1000).WithMessage("生成数量必须在1-1000之间")
                .When(x => x.Count.HasValue);
        }
    }

    /// <summary>
    /// 切换采集状态请求验证器
    /// </summary>
    public class ToggleCollectionRequestValidator : AbstractValidator<Controllers.ToggleCollectionRequest>
    {
        public ToggleCollectionRequestValidator()
        {
            // Boolean属性不需要特殊验证
        }
    }
}